package com.frank.oj.controller.oj;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.model.entity.problem.CodeTemplate;
import com.frank.model.entity.problem.Language;
import com.frank.oj.annotation.AnonApi;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.LastAcceptedCodeVO;
import com.frank.oj.model.dto.PidListDTO;
import com.frank.oj.model.vo.ProblemFullScreenListVO;
import com.frank.oj.model.vo.ProblemInfoVO;
import com.frank.oj.model.vo.ProblemVO;
import com.frank.oj.model.vo.RandomProblemVO;
import com.frank.oj.service.problem.ProblemService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author frank
 * @Date 2024/3/22
 */
@RestController
@RequestMapping("/api")
public class ProblemController {

    @Resource
    private ProblemService problemService;

    /**
     * 获取题目列表
     *
     * @param limit       每页条数
     * @param currentPage 当前页
     * @param keyword     搜索关键字
     * @param tagId       标签
     * @param difficulty  难度
     * @param oj          oj类型
     * @return 题目列表
     */
    @RequestMapping(value = "/get-problem-list", method = RequestMethod.GET)
    @AnonApi
    public CommonResult<Page<ProblemVO>> getProblemList(@RequestParam(value = "limit", required = false) Integer limit,
                                                        @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                        @RequestParam(value = "keyword", required = false) String keyword,
                                                        @RequestParam(value = "tagId", required = false) List<Long> tagId,
                                                        @RequestParam(value = "difficulty", required = false) Integer difficulty,
                                                        @RequestParam(value = "oj", required = false) String oj) {

        return problemService.getProblemList(limit, currentPage, keyword, tagId, difficulty, oj);
    }

    /**
     * 题目状态
     *
     * @param pidListDTO
     * @return
     */
    @PostMapping("/get-user-problem-status")
    @RequiresAuthentication
    public CommonResult<Map<Long, Object>> getUserProblemStatus(@RequestBody @Validated PidListDTO pidListDTO) {
        return problemService.getUserProblemStatus(pidListDTO);
    }


    /**
     * 题目模板
     *
     * @param pid
     * @return
     */
    @GetMapping("/get-problem-code-template")
    @AnonApi
    public CommonResult<List<CodeTemplate>> getProblemCodeTemplate(@RequestParam(value = "pid") Long pid) {
        return CommonResult.successResponse(problemService.getProblemCodeTemplate(pid));
    }

    /**
     * 支持的语言
     *
     * @param pid
     * @return
     */
    @GetMapping("/get-problem-languages")
    @AnonApi
    public CommonResult<Collection<Language>> getProblemLanguages(@RequestParam("pid") Long pid) {
        return CommonResult.successResponse(problemService.getProblemLanguages(pid));
    }

    /**
     * 随机一题
     * @return 题目id
     */
    @GetMapping("/get-random-problem")
    @AnonApi
    public CommonResult<RandomProblemVO> getRandomProblem() {
        return problemService.getRandomProblem();
    }

    /**
     * 获取题目详情
     *
     * @param problemId id
     * @param gid 组id
     * @return 题目信息
     */
    @GetMapping("/get-problem-detail")
    @AnonApi
    public CommonResult<ProblemInfoVO> getProblemInfo(@RequestParam(value = "problemId", required = true) String problemId,
                                                      @RequestParam(value = "gid", required = false) Long gid) {
        return problemService.getProblemInfo(problemId, gid);
    }

    /**
     * 获取用户对于该题最近AC的代码
     */
    @RequiresAuthentication
    @GetMapping("/get-last-ac-code")
    public CommonResult<LastAcceptedCodeVO> getUserLastAcceptedCode(@RequestParam(value = "pid") Long pid,
                                                                    @RequestParam(value = "cid", required = false) Long cid) {
        return problemService.getUserLastAcceptedCode(pid, cid);
    }

    /**
     * 获取专注模式页面底部的题目列表
     */
    @RequiresAuthentication
    @GetMapping("/get-full-screen-problem-list")
    public CommonResult<List<ProblemFullScreenListVO>> getFullScreenProblemList(@RequestParam(value = "tid", required = false) Long tid,
                                                                                @RequestParam(value = "cid", required = false) Long cid) {
        return problemService.getFullScreenProblemList(tid, cid);
    }
}
